#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

static struct option long_options[] =
{
    {"about", no_argument, 0, 'a'},
    {"bus", no_argument, 0, 'b'},
    {"count", required_argument, 0, 'c'},
    {0, 0, 0, 0}
};



void usage() 
{
    printf("Usage: \n");
    printf("\tOptDemo [-a] [-b] [-c message]\n");
}

int main(int argc, char *argv[])
{
    int opt;
    
    //  选项不带冒号表示普通选项，不带参数
    //  带一个冒号表示选项后必须有参数
    //  两个冒号的话，那么这个参数就是可选参数
    // while ( (opt = getopt(argc, argv, "abc::")) != -1)
    while( (opt = getopt_long(argc, argv, "abc::", long_options, NULL)) != -1)
    {
        switch (opt)
        {
        case 'a' :
            /* code */
            printf("opt is a, oprarg is: %s\n", optarg);
            break;
        case 'b':
            /* code */
            printf("opt is b, oprarg is: %s\n", optarg);
            break;
        case 'c':
            /* code */
            printf("opt is c, oprarg is: %s\n", optarg);
            break;
        case '?':
            /* code */
            printf("error optopt: %c\n", optopt);
            printf("error opterr: %d\n", opterr);
            usage();
            break;
        
        default:
            break;
        }
    }
    return 0;
}
